﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TmU220PrintHelperV2d0.Entity;
using ZhangLogSysV1d0;

namespace TmU220PrintHelperV2d0.ControlUi
{
    public partial class PrintTempletElementEditor : UserControl
    {
        public PrintTempletElementEditor()
        {
            InitializeComponent();
            AreaType = AreaTypeV2d0.Header;
            RowIndex = 0;
            IsSelected = false;
        }
        public PrintTempletElementEditor(PrintTempletElementV2d0 printTempletElement,
            AreaTypeV2d0 areaType,int rowIndex,Type printMain,Type printDetail)
        {
            InitializeComponent();
            PrintTempletElement = printTempletElement;
            AreaType = areaType;
            RowIndex = rowIndex;
            IsSelected = false;
            PrintMain = printMain;
            PrintDetail = printDetail;
            Init(PrintTempletElement);
        }
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="printTempletItem">待显示的模板项</param>
        public void Init(PrintTempletElementV2d0 printTempletElement)
        {
            PrintTempletElement = printTempletElement;
            FillDataToForm(PrintTempletElement);
            FillContentComboBox(PrintMain,PrintDetail);
        }
        /// <summary>
        /// 根据模板区域,填充下拉框项
        /// </summary>
        public void FillContentComboBox(Type typeOfPrintMainClass, Type typeOfPrintDetailClass)
        {
            comboBoxElementContent.Items.Clear();

            if (AreaType == AreaTypeV2d0.Detail)
            {
                //foreach (var item in typeof(dp_rms_print_kit_detail_TableClass).GetProperties())
                foreach (var item in typeOfPrintDetailClass.GetProperties())
                {
                    comboBoxElementContent.Items.Add("Detail::" + item.Name);
                }
            }
            else
            {
                //foreach (var item in typeof(dp_rms_print_kit_main_TableClass).GetProperties())
                foreach (var item in typeOfPrintMainClass.GetProperties())
                {
                    comboBoxElementContent.Items.Add("Main::" + item.Name);
                }
            }
        }
        /// <summary>
        /// 数据修改事件
        /// </summary>
        public event EventHandler<int> DataChangedEvent = null;
        /// <summary>
        /// 模板项
        /// </summary>
        public PrintTempletElementV2d0 PrintTempletElement=null;
        public AreaTypeV2d0 AreaType { get; set; }
        public Type PrintMain { get; set; }
        public Type PrintDetail { get; set; }
        /// <summary>
        /// 该元素所属模板行的行序号
        /// </summary>
        public int RowIndex { get; set; }
        public bool IsSelected { get; set; }

        /// <summary>
        /// 保存编辑框内容
        /// </summary>
        public void SaveData()
        {
            SetPrintTempletElementByEditBox();
            if (DataChangedEvent != null) DataChangedEvent(this, RowIndex);
        }
        /// <summary>
        /// 将模板项数据填充到窗口
        /// </summary>
        /// <param name="printTempletItem"></param>
        public void FillDataToForm(PrintTempletElementV2d0 printTempletElement)
        {
            try
            {
                comboBoxElementContent.Text = printTempletElement.ContentText;
                //对齐方式
                if (printTempletElement.HorizontalAlign == "Left") radioButtonAlignLeft.Checked = true;
                else if (printTempletElement.HorizontalAlign == "Center") radioButtonAlignCenter.Checked = true;
                else if (printTempletElement.HorizontalAlign == "Right") radioButtonAlignRight.Checked = true;
                //打印模式
                if (printTempletElement.PrintMode == "Text") radioButtonTextMode.Checked = true;
                else if (printTempletElement.PrintMode == "Image") radioButtonImageMode.Checked = true;
                //颜色
                if (printTempletElement.FontColor == "Black") radioButtonBlack.Checked = true;
                else if (printTempletElement.FontColor == "Red") radioButtonRed.Checked = true;

                //粗体 下划线 倍宽 倍高
                if (printTempletElement.IsBold) checkBoxBold.Checked = true;
                else checkBoxBold.Checked = false;
                if (printTempletElement.IsUnderLine) checkBoxUnderLine.Checked = true;
                else checkBoxUnderLine.Checked = false;
                if (printTempletElement.IsDoubleHeight) checkBoxDoubleHeight.Checked = true;
                else checkBoxDoubleHeight.Checked = false;
                if (printTempletElement.IsDoubleWidth) checkBoxDoubleWidth.Checked = true;
                else checkBoxDoubleWidth.Checked = false;
                //是否分割线
                if (printTempletElement.IsCutoffLine)
                {
                    radioButtonCutoffLine.Checked = true;
                }
                else
                {
                    radioButtonContent.Checked = true;
                }

                numericUpDownFontSize.Value = printTempletElement.FontSize;
            }
            catch (Exception ex)
            {
                SimpleLoger.Instance.Error(ex);
            }
        }

        /// <summary>
        /// 用编辑框数据来设置模板项
        /// </summary>
        public void SetPrintTempletElementByEditBox()
        {
            try
            {
                //内容
                PrintTempletElement.ContentText = comboBoxElementContent.Text;
                //对齐方式
                if (radioButtonAlignLeft.Checked) PrintTempletElement.HorizontalAlign = "Left";
                else if (radioButtonAlignCenter.Checked) PrintTempletElement.HorizontalAlign = "Center";
                else if (radioButtonAlignRight.Checked) PrintTempletElement.HorizontalAlign = "Right";

                //打印模式
                if (radioButtonTextMode.Checked) PrintTempletElement.PrintMode = "Text";
                else PrintTempletElement.PrintMode = "Image";
                //字体颜色
                if (radioButtonRed.Checked) PrintTempletElement.FontColor = "Red";
                else PrintTempletElement.FontColor = "Black";
                //粗体，下划线，倍宽，倍高
                if (checkBoxBold.Checked) PrintTempletElement.IsBold = true;
                else PrintTempletElement.IsBold = false;
                if (checkBoxUnderLine.Checked) PrintTempletElement.IsUnderLine = true;
                else PrintTempletElement.IsUnderLine = false;
                if (checkBoxDoubleWidth.Checked) PrintTempletElement.IsDoubleWidth = true;
                else PrintTempletElement.IsDoubleWidth = false;
                if (checkBoxDoubleHeight.Checked) PrintTempletElement.IsDoubleHeight = true;
                else PrintTempletElement.IsDoubleHeight = false;
                //图像打印模式下的字体大小
                PrintTempletElement.FontSize = (int)numericUpDownFontSize.Value;
                //分隔线
                if (radioButtonCutoffLine.Checked) PrintTempletElement.IsCutoffLine = true;
                else PrintTempletElement.IsCutoffLine = false;
            }
            catch (Exception ex)
            {
                SimpleLoger.Instance.Error(ex);
            }
        }

        /// <summary>
        /// 当点击后，保存数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PrintTempletElementEditor_Click(object sender, EventArgs e)
        {
            IsSelected = true;
            SaveData();
            if (radioButtonCutoffLine.Checked) comboBoxElementContent.Text = new string('-', 40);
            this.Refresh();
        }

        /// <summary>
        /// 下拉框文本改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBoxElementContent_TextChanged(object sender, EventArgs e)
        {
            if (comboBoxElementContent.Text != PrintTempletElement.ContentText)
            {
                IsSelected = true;
                SaveData();
                this.Refresh();
            }
        }

        /// <summary>
        /// 页面刷新事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PrintTempletElementEditor_Paint(object sender, PaintEventArgs e)
        {
            Graphics currGraphics = e.Graphics;
            //绘制选中边框
            Rectangle borderRectangle = this.ClientRectangle;
            if (IsSelected)
            {
                ControlPaint.DrawBorder(e.Graphics, borderRectangle,
                Color.Red, 2, ButtonBorderStyle.Solid, //左边
                Color.Red, 2, ButtonBorderStyle.Solid, //上边
                Color.Red, 2, ButtonBorderStyle.Solid, //右边
                Color.Red, 2, ButtonBorderStyle.Solid);//底边
            }
            else
            {
                ControlPaint.DrawBorder(e.Graphics, borderRectangle,
                Color.Black, 1, ButtonBorderStyle.Solid, //左边
                Color.Black, 1, ButtonBorderStyle.Solid, //上边
                Color.Black, 1, ButtonBorderStyle.Solid, //右边
                Color.Black, 1, ButtonBorderStyle.Solid);//底边
            }
        }

        /// <summary>
        /// 文本模式按钮选中状态改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radioButtonTextMode_CheckedChanged(object sender, EventArgs e)
        {
            if (this.radioButtonTextMode.Checked)
            {
                numericUpDownFontSize.Enabled = false;
                checkBoxDoubleWidth.Enabled = true;
                checkBoxDoubleHeight.Enabled = true;
            }
            else
            {
                numericUpDownFontSize.Enabled = true;
                checkBoxDoubleWidth.Enabled = false;
                checkBoxDoubleHeight.Enabled = false;
            }
        }

        /// <summary>
        /// 图像模式按钮选中状态改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radioButtonImageMode_CheckedChanged(object sender, EventArgs e)
        {
            if (this.radioButtonImageMode.Checked)
            {
                numericUpDownFontSize.Enabled = true;
                checkBoxDoubleWidth.Enabled = false;
                checkBoxDoubleHeight.Enabled = false;
            }
            else
            {
                numericUpDownFontSize.Enabled = false;
                checkBoxDoubleWidth.Enabled = true;
                checkBoxDoubleHeight.Enabled = true;
            }
        }
        /// <summary>
        /// 进入text模式
        /// </summary>
        public void EnterTextMode()
        {
            radioButtonTextMode.Checked = true;
            radioButtonImageMode.Checked = false;
            numericUpDownFontSize.Enabled = false;
            checkBoxDoubleWidth.Enabled = true;
            checkBoxDoubleHeight.Enabled = true;
        }
        /// <summary>
        /// 进入image模式
        /// </summary>
        public void EnterImageMode()
        {
            radioButtonImageMode.Checked = true;
            radioButtonTextMode.Checked = false;
            numericUpDownFontSize.Enabled = true;
            checkBoxDoubleWidth.Enabled = false;
            checkBoxDoubleHeight.Enabled = false;
        }
        /// <summary>
        /// 选中分隔线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radioButtonCutoffLine_Click(object sender, EventArgs e)
        {
            IsSelected = true;
            SaveData();
            if (radioButtonCutoffLine.Checked)
            {
                comboBoxElementContent.Text = new string('-', 40);
                radioButtonImageMode.Checked = false;
                radioButtonTextMode.Checked = true;
            }
            this.Refresh();
        }

        private void radioButtonContent_Click(object sender, EventArgs e)
        {
            IsSelected = true;
            SaveData();
            if (radioButtonContent.Checked) comboBoxElementContent.Text =string.Empty;
            this.Refresh();
        }
    }
}
